

type HierarchyNode = {
    name: string, children: HierarchyNode[], uuid: number
}

let index = 0;

function getHierarchyData(gameObject: GameObject): HierarchyNode {
    //console.log(gameObject.uuid)
    let name = gameObject.id ? gameObject.id : 'Unnamed' + index;
    index++;
    const children = [];
    if (!gameObject.prefabUrl) {
        for (let child of gameObject.children) {
            children.push(getHierarchyData(child));
        }
    }
    else {
        name += "(Prefab)";
    }
    return { name, children, uuid: gameObject.uuid };
}


type BehaviourData = {
    uuid: number,
    name: string,
    gameObject : GameObject;
    properties: { key: string, defaultValue: any, value: any }[]
};


type InspectorData = {
    name: string
    uuid: number
    behaviours: BehaviourData[]
}

function getInspectorData(gameObject: GameObject): InspectorData {
    let name = gameObject.id ? gameObject.id : 'Unnamed';
    const uuid = gameObject.uuid;

    const behaviours: BehaviourData[] = [];

    if (!gameObject.prefabUrl) {
        for (let child of gameObject.$behaviours) {
            const name = child.constructor.name;
            const behaviourProperties = (child.constructor as any).properties || [];
            const properties = behaviourProperties.map(p => {
                return {
                    key: p.key, defaultValue: p.defaultValue, value: child[p.key]
                }
            });
            const uuid = child.uuid;
            const gameObject = child.gameObject
            behaviours.push({ uuid, name, gameObject: gameObject, properties});
        }

        return { uuid, name, behaviours }
    }
    else {
        const objProperties = gameObject.getBehaviour(ChangableValueForPrefab).properties;

        for (let child of objProperties) {
            const name = child.name;

            let properties: BehaviourData['properties'] = [];

            const items = Object.keys(child.parameter);
            for (let item of items) {
                properties.push({ key: item, defaultValue: null, value: child.parameter[item] })
            }
            const uuid = 0;
            const gameobject = child.gameObject
            behaviours.push({ uuid, name, gameObject: gameobject, properties});
        }
        return { uuid, name, behaviours }
    }

}

